package com.android.test.view;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import android.content.Context;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.util.AttributeSet;
import android.util.Log;

import com.androidplot.xy.BarFormatter;
import com.androidplot.xy.BarRenderer;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYStepMode;

public class BarGraph extends XYPlot {

    private static String TAG = "BarGraph";
    private static int DOMAIN_NUMBER = 4;
    private int highLightIndex = 0;

    private ArrayList<Number> Ranges = new ArrayList<Number>();
    private ArrayList<Number> normalList = new ArrayList<Number>();
    private ArrayList<Number> highlightList = new ArrayList<Number>();
    private String domainLabel = "";
    private String rangeLabel = "";
    private String chartNote = "";
    private static String[] domainItemLabel;

    private SimpleXYSeries normalLevelsSeries = new SimpleXYSeries("");

    private SimpleXYSeries highLightHistorySeries = new SimpleXYSeries(
	    "highLight");

    public BarGraph(Context context, AttributeSet attr) {
	super(context, attr);
	// TODO Auto-generated constructor stub
    }

    public BarGraph(Context context, String string) {
	super(context, string);
    }

    @SuppressWarnings("unchecked")
    public void setRanges(ArrayList<Number> inputs) {
	Ranges.clear();
	Ranges = (ArrayList<Number>) inputs.clone();

	splitRange(highLightIndex);
    }

    public void setRanges(Collection<Number> inputs) {
	Ranges.clear();
	if (inputs == null) {
	    return;
	}
	for (Number input : inputs) {
	    Ranges.add(input);
	}
	
	splitRange(highLightIndex);
    }

    @SuppressWarnings("unchecked")
    private void splitRange(int index) {
	// TODO Auto-generated method stub
	if (index < 0 || index >= DOMAIN_NUMBER) {
	    return;
	}
	try {
	    normalList = (ArrayList<Number>) Ranges.clone();
	    normalList.set(index, 0);
	    highlightList = (ArrayList<Number>) Ranges.clone();
	    for (int i = 0; i < DOMAIN_NUMBER; i++) {
		if (i != index) {
		    highlightList.set(i, 0);
		}
	    }
	} catch (Exception e) {

	}

    }

    public void setChartNote(String note) {
	chartNote = note;
    }

    public void setDomainLabel(String label) {
	domainLabel = label;
    }

    public void setRangeLabel(String label) {
	rangeLabel = label;
    }

    public void showChart() {
	if (DOMAIN_NUMBER != Ranges.size()) {
	    return;
	}

	setDomainStepValue(DOMAIN_NUMBER);
	setRangeStepValue(25);
	setRangeStepMode(XYStepMode.INCREMENT_BY_VAL);
	setRangeUpperBoundary(100, BoundaryMode.FIXED);

	setRangeBoundaries(0, 100, BoundaryMode.FIXED);

	// use our custom domain value formatter:
	setDomainValueFormat(new APRIndexFormat());

	// update our domain and range axis labels:
	setDomainLabel(domainLabel);
	getDomainLabelWidget().pack();
	setRangeLabel(rangeLabel);
	getRangeLabelWidget().pack();

	setGridPadding(15, 0, 15, 0);

	normalLevelsSeries.setModel(normalList.subList(0, normalList.size()),
		SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
	addSeries(
		normalLevelsSeries,
		BarRenderer.class,
		new BarFormatter(Color.argb(100, 0, 200, 0), Color
			.rgb(0, 80, 0)));

	highLightHistorySeries.setModel(
		highlightList.subList(0, highlightList.size()),
		SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
	addSeries(highLightHistorySeries, BarRenderer.class, new BarFormatter(
		Color.rgb(200, 100, 100), Color.BLACK));

	BarRenderer barRenderer = (BarRenderer) getRenderer(BarRenderer.class);
	if (barRenderer != null) {
	    barRenderer.setBarWidth(30);
	}
	getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
	getGraphWidget().getGridLinePaint().setColor(Color.BLACK);
	getGraphWidget().getGridLinePaint().setPathEffect(
		new DashPathEffect(new float[] { 1, 1 }, 1));
	getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
	getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);

	disableAllMarkup();
	redraw();
    }

    public int getHighLightIndex() {
	return highLightIndex;
    }

    public void setHighLightIndex(int index) {
	highLightIndex = index;
    }

    public static String[] getDomainItemLabel() {
	return domainItemLabel;
    }

    public static void setDomainItemLabel(String[] strings) {
	if (strings.length != BarGraph.DOMAIN_NUMBER) {
	    return;
	}
	BarGraph.domainItemLabel = strings;
    }
}

class APRIndexFormat extends Format {
    private static final long serialVersionUID = 1L;

    @Override
    public StringBuffer format(Object obj, StringBuffer toAppendTo,
	    FieldPosition pos) {
	Number num = (Number) obj;

	// using num.intValue() will floor the value, so we add 0.5 to round
	// instead:
	int roundNum = (int) (num.floatValue() + 0.5f);
	String appendString = BarGraph.getDomainItemLabel()[roundNum];
	switch (roundNum) {
	case 0:
	case 1:
	case 2:
	case 3:
	    toAppendTo.append(appendString);
	    break;
	default:
	    toAppendTo.append("Unknown");
	}
	return toAppendTo;
    }

    @Override
    public Object parseObject(String source, ParsePosition pos) {
	return null; // We don't use this so just return null for now.
    }
}